<?php
namespace App\Http\Controllers\Api;

use App\Http\Services\RestSms;

class Sms{

    private $accountSid='';
    private $accountToken= '';
    private $appId='';
    private $serverIP='app.cloopen.com';
    private $serverPort='8883';
    private $softVersion='2013-12-26';

    public function __construct()
    {
        $this->accountSid = env('ACCOUNT_SID');
        $this->accountToken = env('AUTH_TOKEN');
        $this->appId = env('APPID');
        $this->serverIP = env('REST_URL');
        $this->serverPort = env('REST_URL_PORT');
        $this->softVersion = env('APP_REST_VERSION');
    }

    function sendTemplateSMS($to,$datas,$tempId)
    {
        // 初始化REST SDK
        $accountSid = $this->accountSid;
        $accountToken = $this->accountToken;
        $appId = $this->appId;
        $serverIP = $this->serverIP;
        $serverPort = $this->serverPort;
        $softVersion = $this->softVersion;
        $rest = new RestSms($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        // 发送模板短信
//        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            return array('code'=>500,'msg'=>'result error');
//            echo "result error!";
//            exit();
        }
        if($result->statusCode!=0) {

            return array('code'=>$result->statusCode,'msg'=>$result->statusMsg);
//            echo "error code :" . $result->statusCode . "<br>";
//            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加错误处理逻辑
        }else{
            return array('code'=>200);
//            echo "Sendind TemplateSMS success!<br/>";
//            // 获取返回信息
//            $smsmessage = $result->TemplateSMS;
//            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
//            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            //TODO 添加成功处理逻辑
        }
    }

}